#include "manager_queue_comm.h"
#include "gateway_manager.h"
#include "led_manager.h"
#include "manager_queue.h"
#include "module_queue_struct.h"
#include "net_app.h"
#include "serial_app.h"
#include "uc_log.h"
#include "wiota_app.h"
#include <rtthread.h>

void *module_get_queue(int module)
{
    void *queue_handler = RT_NULL;

    switch (module)
    {
    case GATEWAY_MANAGER_MODULE:
        queue_handler = get_gateway_manager_queue();
        break;

    case WIOTA_APP_MODULE:
        queue_handler = get_wiota_app_queue();
        break;

    case NET_APP_MODULE:
        queue_handler = net_app_get_queue();
        break;

        // case MANAGER_WEB_MODULE:
        //     break;

        // case LED_MANAGER_MODULE:
        //     queue_handler = get_led_manager_queue();
        //     break;

        // case SERIAL_MANAGER_MODLE:
        //     break;

    case SERIAL_DEBUG_MANAGER_MODULE:
    case BT_MANAGER_MODULE:
    case SERIAL_485_MANAGER_MODULE:
    case SERIAL_UART_MANAGER_MODULE:
        queue_handler = get_serial_queue();
        break;

    case NET_CHANGE_APP_MODULE:
        queue_handler = get_net_change_app_queue();
        break;

    default:
        return RT_NULL;
    }

    return queue_handler;
}

int module_queue_communication(void *queue, int src_module, int dest_module, int cmd, void *data)
{
    int result = 0;
    module_queue_message_t *message;

    if (RT_NULL == queue)
    {
        TRACE_E("queue is null,send %d err", cmd);
        return -1;
    }

    message = rt_malloc(sizeof(module_queue_message_t));
    RT_ASSERT(message);

    message->src_task = (manager_tasks_identification_e)(src_module);
    message->dest_task = (manager_tasks_identification_e)(dest_module);
    message->cmd = cmd;
    message->data = data;

    result = manager_send_queue(queue, message, MODULE_SEND_QUEUE_TIMEOUT);
    if (RT_EOK != result)
    {
        rt_free(message);
        TRACE_E("manager_send_queue error, src_task %d, cmd 0x%x, result %d\n", src_module, message->cmd, result);
        return 1;
    }

    return 0;
}

int module_queue_send_static_data(void *queue, int src_module, int dest_module, int cmd, void *data, void *msg)
{
    module_queue_message_t *message = (module_queue_message_t *)msg;

    message->src_task = (manager_tasks_identification_e)(src_module);
    message->dest_task = (manager_tasks_identification_e)(dest_module);
    message->cmd = cmd;
    message->data = data;

    return manager_send_queue(queue, message, 0);
}
